package com.luck.pf.security.param.logic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.luck.org.entity.SysLog;
import com.luck.org.entity.SysParams;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.security.param.qry.ParamQry;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.Page;

/**
 * 说明: Copyright: Copyright (c) 2010
 * 
 * @author zhangwr 2011-10-11 下午03:28:19
 * @version 1.0
 */
public class ParamLogic extends BaseLogic implements IParamLogic {
	private BaseJdbcDao jdbcDao;

	public Page findParamPage(ParamQry qry) throws Exception {
		Criteria criteria = super.baseDao.createCriteria(SysParams.class);
		CriteriaUtil.addFullLike(criteria, "paramCode", qry.getParamCode());
		CriteriaUtil.addFullLike(criteria, "paramName", qry.getParamName());
		CriteriaUtil.addEq(criteria, "paramStatus", qry.getParamStatus());
		//CriteriaUtil.addEq(criteria, "paramType", qry.getParamType());
		CriteriaUtil.addOrder(criteria, "paramCode", 1);
		CriteriaUtil.addEq(criteria, "paramType", "1");
		CriteriaUtil.addOrder(criteria, "id", 1);
		criteria.createAlias("parentParam", "p",1);
		//criteria.add(Restrictions.isNotNull("p.id"));
		CriteriaUtil.addEq(criteria, "p.id", qry.getParentId());
		return super.baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}
	public List<SysParams> findTopParams(){
		return super.baseDao.find(" FROM SysParams WHERE parentParam.id is null and paramType =1");
	}
	public String saveParam(SysParams param, UserBean userBean)throws Exception {
		if(param.getParentParam()!=null)
			param.setParamCode(param.getParentParam().getParamCode());
		super.baseDao.save(param);
		return null;
	}

	public String deleteParam(SysParams param, UserBean userBean, SysLog log)throws Exception {
		StringBuffer bf = new StringBuffer();
		int sons = super.baseDao.count(" SELECT count(*) from SysParams where parentParam.id=?", param.getId());
		if(sons>0)
			bf.append("此参数有子参数<br/>");
		if(StringUtils.isBlank(bf.toString()))
			super.baseDao.delete(param);
		else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}
